home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-03
/
imb9101.zip
/
GETKEY.BAS
< prev
next >
Wrap
BASIC Source File
|
1990-12-31
|
3KB
|
151 lines
DECLARE FUNCTION IsSpecialKey% (KeyCode%)
DECLARE FUNCTION GetKey% ()
DECLARE FUNCTION UngetKey% (KeyCode%)
'Program==GETKEY.BAS==Functions to let you get keys from
' the keyboard, even arrows and function keys.
' Includes a keypress pushback mechanism.
DEFINT A-Z
CLS
CONST MaxKeys = 16
DIM SHARED UgkIdx%, True%, False%, UgkBuf(MaxKeys)
UgkIdx = 0
True% = -1
False% = 0
'Definitions for special keys
CONST Null = 0
CONST kF1 = 315
CONST kF2 = 316
CONST kF3 = 317
CONST kF4 = 318
CONST kF5 = 319
CONST kF6 = 320
CONST kF7 = 321
CONST kF8 = 322
CONST kF9 = 323
CONST kF10 = 324
CONST kHome = 327
CONST kEnd = 335
CONST kPgUp = 329
CONST kPgDn = 337
CONST kIns = 338
CONST kDel = 339
CONST kUp = 328
CONST kDown = 336
CONST kleft = 331
CONST kRight = 333
CONST kEnter = 13
CONST kEsc = 27
'** Demonstration of the GetKey routines ************************
'**** Pushback the message "Bob!"
A% = UngetKey%(ASC("!"))
A% = UngetKey%(ASC("b"))
A% = UngetKey%(ASC("o"))
A% = UngetKey%(ASC("B"))
WHILE I <> kEnd
I = GetKey%
SELECT CASE I
CASE Null
CASE kF1
PRINT "<F1>"
CASE kPgUp
PRINT "<PgUp>"
CASE kEnd
PRINT "<End>"
CASE kEnter
PRINT "<Enter>"
CASE kEsc
PRINT "<Esc>"
CASE ELSE
IF IsSpecialKey%(I) THEN
PRINT USING "[###]"; I
ELSE
PRINT CHR$(I)
END IF
END SELECT
WEND
END
FUNCTION GetKey%
'****************************************************************
'** GetKey - Get a key from the keyboard (or Unget buffer if **
'** it's not empty). **
'** Output - Value of next available key **
'****************************************************************
'** Return a key that has be Ungotten, if one's there
IF UgkIdx > 0 THEN
GetKey% = UgkBuf(UgkIdx)
UgkIdx = UgkIdx - 1
ELSE
C$ = INKEY$
IF LEN(C$) = 0 THEN
GetKey% = 0
ELSEIF LEN(C$) = 1 THEN
GetKey% = ASC(C$)
ELSEIF LEN(C$) = 2 THEN
GetKey% = 256 + ASC(RIGHT$(C$, 1))
END IF
END IF
END FUNCTION
FUNCTION IsSpecialKey% (KeyCode%)
'****************************************************************
'** SpecialKey - decides if the specified value represents a **
'** special key or not. (Exm. F3, Up arrow, **
'** Page down, etc.) **
'** Output - True if it's a special key, otherwise False **
'****************************************************************
IsSpecialKey% = False
IF KeyCode% > 255 OR KeyCode% < 0 THEN
IsSpecialKey% = True
END IF
END FUNCTION
FUNCTION UngetKey% (KeyCode%)
'****************************************************************
'** UngetKey - push a key back into the keyboard buffer. *
'** Input - KeyCode% - Key to push back *
'** Output - 0 if failed, -1 if successful *
'****************************************************************
IF UgkIdx < MaxKeys THEN
UgkIdx = UgkIdx + 1
UgkBuf(UgkIdx) = KeyCode%
UngetKey% = True
ELSE
UngetKey% = False
END IF
END FUNCTION